package controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class Outils {

	
	public static boolean checkIp (String sip)
    {
        String [] parts = sip.split ("\\.");
        if (parts.length != 4) return false;
        for (String s : parts)
        {
            int i = Integer.parseInt (s);
            if (i < 0 || i > 255)
            {
                return false;
            }
        }
        return true;
    } 
	
	public static boolean checkPort (int port)
	{
		
		if(port < 0 || port > 65535) return false;
		return true;
	}
	
	public static InetAddress getInetAddress(String ip){
		String [] parts = ip.split ("\\.");
		byte[] bytes = new byte[4];
		bytes[0]=(byte) Integer.parseInt(parts[0]);
		bytes[1]=(byte) Integer.parseInt(parts[1]);
		bytes[2]=(byte) Integer.parseInt(parts[2]);
		bytes[3]=(byte) Integer.parseInt(parts[3]);
		
		InetAddress result=null;
		try {
			result = InetAddress.getByAddress(bytes);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static Properties load(URL url) throws IOException, FileNotFoundException{
	      Properties properties = new Properties();
	         properties.load(url.openStream());
	         return properties;

	   }
	
	public static List<String> getIps(){
		List<String> ips = new ArrayList<String>();
		
		try{
			Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
		                
	         while (interfaces.hasMoreElements()) {  // carte reseau trouvee
	            NetworkInterface interfaceN = (NetworkInterface)interfaces.nextElement(); 
	            Enumeration<InetAddress> ienum = interfaceN.getInetAddresses();
	            while (ienum.hasMoreElements()) {  // retourne l adresse IPv4 et IPv6
	                InetAddress ia = ienum.nextElement();
	                String adress = ia.getHostAddress().toString();
	                
	                if( adress.length() < 16){          //On s'assure ainsi que l'adresse IP est bien IPv4
	                    if(adress.startsWith("127")){  //Ce n'est pas l'adresse IP Local' 
	                        //System.out.println(ia.getHostAddress());
	                    }
	                    else if(adress.indexOf(":") > 0){
	                       // System.out.println(ia.getHostAddress()); // les ":" indique que c'est une IPv6"
	                    }
	                } 
	                
	                ips.add(adress);        
	            }
	        }
	    }
	    catch(Exception e){
	        System.out.println("pas de carte reseau");
	        e.printStackTrace();
	    }
	    
	    return ips;
	}

	
}
